package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ReportBriefElement;
import com.ruoyi.system.domain.ReportElement;
import com.ruoyi.system.domain.vo.BriefReportVo;
import com.ruoyi.system.mapper.ReportElementMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BriefReportMapper;
import com.ruoyi.system.domain.BriefReport;
import com.ruoyi.system.service.IBriefReportService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 简报模板Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-07
 */
@Service
public class BriefReportServiceImpl implements IBriefReportService 
{
    @Autowired
    private BriefReportMapper briefReportMapper;

    @Autowired
    private ReportElementMapper reportElementMapper;

    /**
     * 查询简报模板
     * 
     * @param briefReportId 简报模板ID
     * @return 简报模板
     */
    @Override
    public BriefReportVo selectBriefReportById(Long briefReportId)
    {
        BriefReportVo vo=new BriefReportVo();
        BriefReport report= briefReportMapper.selectBriefReportById(briefReportId);
        BeanUtils.copyProperties(report,vo);
        List<ReportElement> elements= briefReportMapper.selectElementsByReportId(briefReportId);
        vo.setElements(elements);
        return vo;
    }

    /**
     * 查询简报模板列表
     * 
     * @param briefReport 简报模板
     * @return 简报模板
     */
    @Override
    @DataScope(deptAlias = "template",userAlias = "template")
    public List<BriefReport> selectBriefReportList(BriefReport briefReport)
    {
        return briefReportMapper.selectBriefReportList(briefReport);
    }

    /**
     * 新增简报模板
     * 
     * @param briefReport 简报模板
     * @return 结果
     */
    @Override
    public int insertBriefReport(BriefReport briefReport)
    {
        briefReport.setCreateTime(DateUtils.getNowDate());
        briefReport.setUpdateTime(DateUtils.getNowDate());
        return briefReportMapper.insertBriefReport(briefReport);
    }

    /**
     * 修改简报模板
     * 
     * @param briefReport 简报模板
     * @return 结果
     */
    @Override
    public int updateBriefReport(BriefReport briefReport)
    {
        briefReport.setUpdateTime(DateUtils.getNowDate());
        return briefReportMapper.updateBriefReport(briefReport);
    }

    /**
     * 批量删除简报模板
     * 
     * @param briefReportIds 需要删除的简报模板ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBriefReportByIds(Long[] briefReportIds)
    {
        //删除关联数据
        briefReportMapper.deleteReportBriefElementByIds(briefReportIds);
        return briefReportMapper.deleteBriefReportByIds(briefReportIds);
    }

    /**
     * 删除简报模板信息
     * 
     * @param briefReportId 简报模板ID
     * @return 结果
     */
    @Override
    public int deleteBriefReportById(Long briefReportId)
    {
        return briefReportMapper.deleteBriefReportById(briefReportId);
    }


    @Override
    @Transactional
    public int createTemplate(BriefReportVo briefReport) {
        briefReport.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        briefReport.setIsSystem(0);
        int res=insertBriefReport(briefReport);
        List<ReportBriefElement> list=new ArrayList<>();
        for (ReportElement relation:briefReport.getElements()) {
            ReportBriefElement entity=new ReportBriefElement();
            entity.setBriefId(briefReport.getBriefReportId());
            entity.setElementId(relation.getId());
            entity.setSequence(relation.getIndex());
            entity.setShowName(relation.getShowName());
            entity.setText(relation.getText());
            list.add(entity);
        }
        return briefReportMapper.batchBriefElement(list);
    }

    @Override
    public int setDefaultTemplate(Long briefReportId) {
        return briefReportMapper.setDefaultTemplate(briefReportId);
    }

    @Override
    public BriefReportVo getDefaultTemplate() {
        BriefReportVo vo=new BriefReportVo();
        BriefReport report=  briefReportMapper.getDefaultTemplate();
        BeanUtils.copyProperties(report,vo);
        List<ReportElement> elements= briefReportMapper.selectElementsByReportId(report.getBriefReportId());
        vo.setElements(elements);
        return vo;
    }
}
